package com.huawei.crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huawei.crm.domain.Customer;
import com.huawei.crm.domain.CustomerTransfer;
import com.huawei.crm.domain.Employee;
import com.huawei.crm.mapper.CustomerMapper;
import com.huawei.crm.mapper.CustomertransferMapper;
import com.huawei.crm.query.QueryObject;
import com.huawei.crm.service.ICustomerTransferService;
import com.huawei.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CustomerTransferServiceImpl extends AbstractService<CustomertransferMapper, CustomerTransfer, Long>
        implements ICustomerTransferService {

    @Autowired
    private CustomertransferMapper mapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public void saveCustomerTransfer(CustomerTransfer entity) {
        Employee currentEmp = UserContext.getCurrentEmp();
        entity.setOperator(currentEmp);
        entity.setOperateTime(new Date());

        mapper.insert(entity);

        //更新当前客户的营销人员
        customerMapper.updateSellerById(entity.getNewSeller().getId(), entity.getCustomer().getId());


        //修改客户的状态
        customerMapper.updateStatus(entity.getCustomer().getId(), Customer.STATUS_POTENTIAL);
    }

    @Override
    public PageInfo<CustomerTransfer> query(QueryObject qo) {
        // 告诉mybatis需要去哪里拼接sql
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), "his.operate_time DESC");
        List<CustomerTransfer> list = mapper.selectForList(qo);
        return new PageInfo<>(list);
    }
}
